from live_cals import Main_func
import gc
import json
import time
import datetime


def cache_Loop(codeL=[], pre=""):
    while True:
        with open(r"config_live.json", mode="r") as f:
            res = f.read()
        dic = json.loads(res)
        print()
        for i in dic:
            print("{} {}".format(i, dic[i]))
        if codeL == []:
            codeL = dic["updating_code_list"]
        elif isinstance(codeL, str):
            codeL = [codeL]
        else:
            codeL =codeL[:2]
        if pre == "":
            pre = dic["Dropdown_pre"]
        for code in codeL:
            Main_func.Main_cal(
                pre=pre,
                code=code,
                future_days=5,
            )
        gc.collect()
        now = int(datetime.datetime.now().strftime("%H%M%S"))
        print(now)
        if now > 160000 or len(codeL) == 1:
            break
        else:
            sleeping = 20
            print("sleeping *** {}".format(sleeping))
            time.sleep(sleeping)


if __name__ == "__main__":
    cache_Loop()
    cache_Loop(codeL=[], pre="120")
    cache_Loop(codeL=[], pre="60")
    cache_Loop(codeL=[], pre="30")
    cache_Loop(codeL=[], pre="15")
    cache_Loop(codeL=[], pre="5")
